package com.jinniulive.platform.activity;

import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.jinniulive.platform.R;
import com.jinniulive.platform.module.home.HomeFragment;
import com.jinniulive.platform.module.live.LiveFragment;
import com.jinniulive.platform.module.me.MeFragment;
import com.jinniulive.platform.module.news.NewsFragment;
import com.jinniulive.platform.module.video.VideoFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup mRadioGroup;
    private List<Fragment> mFragments;
    private FragmentManager mManager;

    @Override
    protected void initView() {
        initViews();
        initData();
        setData();
        setListener();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    private void setListener() {
        mRadioGroup.setOnCheckedChangeListener(this);
    }

    private void setData() {
        mFragments.add(new HomeFragment());
        mFragments.add(new LiveFragment());
        mFragments.add(new VideoFragment());
        mFragments.add(new NewsFragment());
        mFragments.add(new MeFragment());
        FragmentTransaction transaction = mManager.beginTransaction();
        for (int i = 0; i < mFragments.size(); i++) {
            transaction.add(R.id.main_fl, mFragments.get(i));
            transaction.hide(mFragments.get(i));
        }
        transaction.show(mFragments.get(0));
        transaction.commit();
    }

    private void initData() {
        mFragments = new ArrayList<>();
    }

    private void initViews() {
        mRadioGroup = (RadioGroup) findViewById(R.id.main_rg);
        ((RadioButton) mRadioGroup.getChildAt(0)).setChecked(true);
        mManager = getSupportFragmentManager();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int index = 0;
        switch (checkedId) {
            case R.id.main_rb_home:
                index = 0;
                break;
            case R.id.main_rb_live:
                index = 1;
                break;
            case R.id.main_rb_video:
                index = 2;
                break;
            case R.id.main_rb_news:
                index = 3;
                break;
            case R.id.main_rb_me:
                index = 4;
                break;
            default:
                break;
        }
        //显示隐藏 fragmnet
        switchFragment(index);
    }

    private void switchFragment(int index) {
        FragmentTransaction transaction = mManager.beginTransaction();
        for (int i = 0; i < mFragments.size(); i++) {
            if (index == i) {
                transaction.show(mFragments.get(i));
            } else {
                transaction.hide(mFragments.get(i));
            }
        }
        transaction.commit();
    }

    boolean isFinish = false;

    @Override
    public void onBackPressed() {
        if (isFinish) {
            super.onBackPressed();
        } else {
            isFinish = true;
            Toast.makeText(this, "再按一次退出今牛直播", Toast.LENGTH_SHORT).show();
            new Handler().postDelayed(new Runnable() {
                                          @Override
                                          public void run() {
                                              isFinish = false;
                                          }
                                      }, 2500
            );

        }

    }


}
